<!--
 * @Author       : ionluo(2437403125@qq.com)
 * @Description  :
 * @Date         : 2024-05-11 22:32:09
-->

<template>
  <div class="sales-view">
    <el-card shadow="hover">
      <template v-slot:header>
        <div class="menu-wrapper">
          <el-menu class="sales-view-menu" mode="horizontal"
                   :default-active="activeIndex" @select="onMenuSelect">
            <el-menu-item index="1">销售额</el-menu-item>
            <el-menu-item index="2">访问量</el-menu-item>
          </el-menu>
          <div class="menu-right">
            <el-radio-group v-model="activeLabel" size="small">
              <el-radio-button label="今日"></el-radio-button>
              <el-radio-button label="本周"></el-radio-button>
              <el-radio-button label="本月"></el-radio-button>
              <el-radio-button label="今年"></el-radio-button>
            </el-radio-group>
            <!-- 不联动切换开始/结束日期 -->
            <el-date-picker class="sales-view-date-picker" type="daterange" v-model="selectDate"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
              size="small" :picker-options="datePickerOptions" unlink-panels
            ></el-date-picker>
          </div>
        </div>
      </template>
      <template>
        <el-row class="sales-view-chart-wrapper" :gutter="20">
          <el-col :lg="16">
            <v-chart :options="chartOptions" :autoresize="true"/>
          </el-col>
          <el-col :lg="8">
            <div class="sales-view-list">
              <div class="sales-view-title">排行榜</div>
              <div class="list-item-wrapper">
                <div class="list-item" v-for="item in rankData" :key="item.no">
                  <div class="list-item-no" :class="{'top-no': +item.no <= 3}">
                    {{ item.no }}
                  </div>
                  <div class="list-item-name">
                    {{ item.name }}
                  </div>
                  <div class="list-item-money">
                    {{ item.money }}
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'SalesView',
  data () {
    return {
      activeIndex: '1',
      activeLabel: '今日',
      selectDate: null,
      rankData: [ // 排行榜
        { no: 1, name: '麦当劳', money: '323, 234' },
        { no: 2, name: '麦当劳', money: '323, 234' },
        { no: 3, name: '麦当劳', money: '323, 234' },
        { no: 4, name: '麦当劳', money: '323, 234' },
        { no: 5, name: '麦当劳', money: '323, 234' },
        { no: 6, name: '麦当劳', money: '323, 234' },
        { no: 7, name: '麦当劳', money: '323, 234' }
      ],
      datePickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date().setTime(end.getTime() - 1000 * 60 * 60 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date().setTime(end.getTime() - 1000 * 60 * 60 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date().setTime(end.getTime() - 1000 * 60 * 60 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      chartOptions: {
        title: {
          text: '年度销售额',
          textStyle: {
            fontSize: 12,
            // fontWeight: 500,
            color: '#666'
          }
          // left: 0 // 调整标题的位置
          // top: 20
        },
        xAxis: {
          type: 'category',
          data: [...new Array(12).keys()].map(item => `${item + 1}月`),
          axisTick: { // x轴刻度修改
            alignWithLabel: true, // x轴刻度对齐柱子
            lineStyle: {
              color: '#999'
            }
          },
          axisLine: { // x轴线条修改
            lineStyle: {
              color: '#999'
            }
          },
          axisLabel: { // x轴标签修改（上面修改axisLine会导致label也一起变成灰色）
            color: '#333'
          }
        },
        yAxis: {
          axisLine: {
            show: false // 隐藏y轴的竖线
          },
          axisTick: {
            show: false // 隐藏y轴刻度
          },
          splitLine: { // 修改y轴辅助线样式
            lineStyle: {
              type: 'dotted',
              color: '#eee'
            }
          }
        },
        series: [{
          type: 'bar',
          data: [200, 250, 300, 350, 300, 250, 200, 250, 300, 350, 300, 250],
          barWidth: '35%',
          color: ['#3398DB']
        }],
        grid: {
          top: 50,
          left: 40,
          right: 60,
          bottom: 50
        }
      }
    }
  },
  methods: {
    onMenuSelect (index) {
      this.activeIndex = index
    }
  }
}
</script>

<style lang="scss" scoped>
.sales-view {
  .menu-wrapper {
    position: relative;
    display: flex;

    .sales-view-menu {
      width: 100%;
      padding-left: 20px;

      /deep/ .el-menu-item {
        height: 50px;
        line-height: 50px;
        margin: 0 20px;
      }
    }

    .menu-right {
      display: none;
      @media (min-width: 992px) {
        display: flex;
      }
      position: absolute;
      right: 20px;
      height: 50px;
      align-items: center;
      justify-content: flex-end;

      .sales-view-date-picker {
        margin-left: 20px;
      }
    }
  }

  .sales-view-chart-wrapper {
    > * { height: 255px; }
    .sales-view-list {
      .sales-view-title {
        font-size: 12px;
        color: #666;
        font-weight: 500;
      }
      .list-item-wrapper {
        margin-top: 15px;
        .list-item  {
          display: flex;
          align-items: center;
          font-size: 12px;
          height: 20px;
          padding: 6px 20px 6px 0;

          .list-item-no {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 20px;
            height: 20px;
            color: #333;

            &.top-no {
              background: #000;
              color: #fff;
              border-radius: 50%;
              font-weight: 500;
            }
          }

          .list-item-name {
            margin-left: 10px;
            color: #333;
          }

          .list-item-money {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>
